
#include <algorithm>
#include <iterator>
#include <thread>
#include <vector>
#include <string>
#include <functional>
#include <iostream>
#include <sstream>
#include <iterator>
#include <map>

using namespace std;

static void t1(vector<string> vdt)
{
	copy(vdt.begin(),vdt.end(),ostream_iterator<string>(cout, "\t"));
}

void mergeData()
{
	vector<string> vdt = {"11","22"};
	vector<string> vdt2 = { "33", "44" };
	
	vdt.insert(vdt.end(), vdt2.begin(), vdt2.end());
}


static void simpleFun(string a)
{
	cout << this_thread::get_id() << " ret:" << a << endl;
}


template<typename valuetype>
struct compare
{
	bool operator()(valuetype value1, valuetype value2)
	{
		return false;
	}
};

// C++11返回类型 推导增加可读性[9/16/2016 xystar]
auto pf1() ->auto(*)()->int(*)()
{
	return nullptr;
}


static void mapTest()
{
	// , greater<int>
	std::map<int, string > mdt;

	mdt.insert({1, "aa"});
	mdt.insert({ 3, "bb" });
	mdt.insert({ 5, "bb" });
	mdt.insert({ 6, "bb" });
	mdt.insert({ 4, "bb" });
	mdt.insert({ 2, "bb" });

	for (auto it : mdt)
	{
		cout << it.first << (" ") << it.second << endl;
	}
}


bool __inline HasTouched(RECT &rcItem, POINT ptBegin, POINT ptEnd)
{
	int yMax = max(ptBegin.y, ptEnd.y);
	int yMin = min(ptBegin.y, ptEnd.y);

	return ((yMax >= rcItem.bottom && yMin < rcItem.bottom) || (yMax >= rcItem.top && yMin < rcItem.top));
}

void static clockTest()
{
    std::function<void ()> f = []()
    {
        volatile double d = 0;
        for(int n=0; n<10000; ++n)
           for(int m=0; m<10000; ++m)
               d += d*n*m;
    };

    std::clock_t c_start = std::clock();
    auto t_start = std::chrono::high_resolution_clock::now();
    std::thread t1(f);
    std::thread t2(f); // f() is called on two threads
    t1.join();
    t2.join();
    std::clock_t c_end = std::clock();
    auto t_end = std::chrono::high_resolution_clock::now();
 
    std::cout << std::fixed << std::setprecision(2) << "CPU time used: "
              << 1000.0 * (c_end-c_start) / CLOCKS_PER_SEC << " ms\n"
              << "Wall clock time passed: "
              << std::chrono::duration<double, std::milli>(t_end-t_start).count()
              << " ms\n";
}

static void millSec()
{
    struct timeb tb;
    int nLoop = 0;

    do
    {
        ftime(&tb);
        printf("UTC:   %s", asctime(gmtime(&tb.time)));
        printf("local: %s", asctime(localtime(&tb.time)));
        printf(".%03d\n",tb.millitm);
        std::this_thread::sleep_for(milliseconds(1000*1 + nLoop*10));
        char mbstr[100];
        if (std::strftime(mbstr, sizeof(mbstr), "%Y-%m-%d %H:%M:%S", std::localtime(&tb.time))) {
            std::cout << mbstr << '\n';
        }
    
    }while(++nLoop < 3);
}

static __inline void ClientToScreen(HWND wnd, RECT &rect)
{
	::ClientToScreen(wnd, (LPPOINT)&rect);
	::ClientToScreen(wnd, (LPPOINT)&rect + 1);
}

void getCurrentTime()    
{    
    struct timeb tb;
    ftime(&tb);
    cout << "millTm:" << tb.millitm << endl;

    printf("time_ty:%lu,Local time1 is %s\n",tb.time,asctime(localtime(&tb.time)));
}

static int  active_add()
{
    ActiveAdder adder;

    ActiveResult<int> sum = adder.add(std::make_pair(1, 2));
    // do other things
    sum.wait();
    std::cout << sum.data() << std::endl;

    return 0;
} 

int main(int argc, char* argv[])
{
	
	mapTest();
	
	mergeData();

	vector<string> vdt;
	vdt.push_back("aa");
	vdt.push_back("bb");

	// thread t1(std::bind(t1, vdt));
	thread t1(std::bind(simpleFun, "hello world"));
	t1.join();

	cout << "main thread end\n";


	return 0;
}
